extends Node2D
class_name Sequence


var nodes:Array
var isPlaying = false
var index = 0
var doneDict = {}


func setNodes(_nodes):
	nodes = _nodes
	
	
func _physics_process(delta: float) -> void:
	if not isPlaying: return
	
	var current = nodes[index]
	if current is Array:
		var isAllDone = true
		for node in current:
			if not doneDict.has(node):
				doneDict[node] = false
			if not doneDict[node]:
				var f = node.callFunc
				var result
				if node.params != null:
					result = f.call(node.params)
				else:
					result = f.call()
				if not result: 
					isAllDone = false
				else:
					doneDict[node] = true
		if isAllDone:
			doneDict = {}
			index += 1
			if index >= nodes.size():
				stop()
	else:
		var f = current.callFunc
		var isDone 
		if current.params != null:
			isDone = f.call(current.params)
		else:
			isDone = f.call()
		if isDone:
			index += 1
			if index >= nodes.size():
				stop()
	
	
func play():
	isPlaying = true
	

func pause():
	isPlaying = false
	

func stop():
	isPlaying = false
	index = 0


	
